<template>
    <div class="slide-banner">
        <div class="banner-wrapper">
            <div class="slide-banner-wrapper" ref="slide">
                <div class="slide-banner-content">
                    <img v-for="item in img_list" :src="item.src" :class="'page' + item.img_class" :key="item.id" alt="#">
                </div>
            </div>
            <div class="dots-wrapper">
                <span class="dot" v-for="num in nums" :key="num"
                    :class="{'active': currentPageIndex === (num - 1)}"></span>
            </div>
        </div>
    </div>
</template>

<script type="text/ecmascript-6">
import BScroll from '@better-scroll/core'
import Slide from '@better-scroll/slide'

BScroll.use(Slide)

export default {
    data() {
        return {
            nums: 4,
            currentPageIndex: 0,
            img_list:[
                {
                    id:1,
                    src:'https://imgs.qunarzz.com/vs_ceph_vcimg/5be2601d1f921f166646b817459a2727.jpeg',
                    img_class:'1'
                },
                {
                    id:2,
                    src: 'https://imgs.qunarzz.com/vs_ceph_vcimg/280673be5655fc91ce24ba605c62fe07.jpeg',
                    img_class:'2'
                }
            ]
        }
    },
    mounted() {
        this.init()
    },
    beforeDestroy() {
        this.slide.destroy()
    },
    methods: {
        init() {
            this.slide = new BScroll(this.$refs.slide, {
                scrollX: true,
                scrollY: false,
                slide: true,
                momentum: false,
                bounce: false,
                probeType: 3
            })
            this.slide.on('scrollEnd', this._onScrollEnd)

            this.slide.on('slideWillChange', (page) => {
                this.currentPageIndex = page.pageX
            })

            // v2.1.0
            this.slide.on('slidePageChanged', (page) => {
                console.log('CurrentPage changed to => ', page)
            })
        },
        _onScrollEnd() {
            console.log('CurrentPage => ', this.slide.getCurrentPage())
        },
        nextPage() {
            this.slide.next()
        },
        prePage() {
            this.slide.prev()
        }
    }
}
</script>

<style lang="scss" scoped>
$color_1: #fff;
$background-color_1: #95b8d1;
$background-color_2: #dda789;
$background-color_3: #c3d899;
$background-color_4: #f2d4a7;
$background-color_5: #666;

.slide-banner {
    .banner-wrapper {
        position: relative;
    }

    .slide-banner-wrapper {
        min-height: 1px;
        overflow: hidden;
    }

    .slide-banner-content {
        white-space: nowrap;
        font-size: 0;

        .slide-page {
            display: inline-block;
            height: 200px;
            width: 100%;
            line-height: 200px;
            text-align: center;
            font-size: 26px;
        }

        .slide-page.page1 {
            background-color: $background-color_1;
        }

        .slide-page.page2 {
            background-color: $background-color_2;
        }

        .slide-page.page3 {
            background-color: $background-color_3;
        }

        .slide-page.page4 {
            background-color: $background-color_4;
        }
    }

    .dots-wrapper {
        position: absolute;
        bottom: 4px;
        left: 50%;
        transform: translateX(-50%);

        .dot {
            display: inline-block;
            margin: 0 4px;
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: #eee;
        }

        .dot.active {
            width: 20px;
            border-radius: 5px;
        }
    }

    .btn-wrap {
        margin-top: 20px;
        display: flex;
        justify-content: center;

        button {
            margin: 0 10px;
            padding: 10px;
            color: $color_1;
            border-radius: 4px;
            background-color: $background-color_5;
        }
    }
}
</style>